package gui;

import io.red.GestorInterfacesRed;
import java.awt.Color;
import java.util.List;
import javax.swing.JOptionPane;
import logica.Verificador;

/**
 *
 * @author Mariano Zapata
 */
public class PanelConfiguracionRed extends javax.swing.JPanel {
    
    /** Creates new form PanelConfiguracionRed */
    public PanelConfiguracionRed(String ipServidor, String puertoRMIServidor, String puertoRMILocal,
            List<String> tarjetasRed, String interfaceConfigurada, int tipoIP)
    {
        initComponents();
        this.puertoRMIServidor.setText(puertoRMIServidor);
        this.puertoRMILocal.setText(puertoRMILocal);
        this.ipServidor.setText(ipServidor);
        for (String s : tarjetasRed)
            interfacesRed.addItem(s);
        if (!interfaceConfigurada.isEmpty())
            interfacesRed.setSelectedItem(interfaceConfigurada);
        if (tipoIP == GestorInterfacesRed.IPV4)
            tipoIPv4.setSelected(true);
        else
            tipoIPv6.setSelected(true);
    }
    
    public String getIpServidor() {
        return ipServidor.getText();
    }
    
    public String getPuertoRMIServidor() {
        return puertoRMIServidor.getText();
    }
    
    public String getPuertoRMILocal() {
        return puertoRMILocal.getText();
    }
    
    public String getInterfaceRedSeleccionada() {
        return (String) interfacesRed.getSelectedItem();
    }
    
    public int getTipoIP()
    {
        if (tipoIPv4.isSelected())
            return GestorInterfacesRed.IPV4;
        else
            return GestorInterfacesRed.IPV6;
    }
    
    public void quitarErrores()
    {
        puertoRMIServidor.setBackground(Color.white);
        puertoRMILocal.setBackground(Color.white);
    }
    
    public void marcarErrores(List<Integer> errores)
    {
        for (int i : errores)
        {
            switch (i)
            {
                case Verificador.ERROR_PUERTO_RMI_REMOTO: puertoRMIServidor.setBackground(Color.red); break;
                case Verificador.ERROR_PUERTO_RMI_LOCAL: puertoRMILocal.setBackground(Color.red); break;
            }
        }
        JOptionPane.showMessageDialog(this, "Se detectaron campos incorrectos.\n\nPara obtener ayuda sobre el formato " +
                "correcto posicione el mouse sobre el campo por un instante.", "Error", JOptionPane.ERROR_MESSAGE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupo = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        puertoRMIServidor = new javax.swing.JTextField();
        puertoRMILocal = new javax.swing.JTextField();
        ipServidor = new javax.swing.JTextField();
        interfacesRed = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        tipoIPv4 = new javax.swing.JCheckBox();
        tipoIPv6 = new javax.swing.JCheckBox();

        jLabel1.setText("Puerto para servicio RMI del Servidor:");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Puerto para servicio RMI local:");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Dirección IP o DNS del Servidor:");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Interface de red:");

        puertoRMIServidor.setToolTipText("Formato: número entero en el intervalo [1025;65536]");

        puertoRMILocal.setToolTipText("Formato: número entero en el intervalo [1025;65536]");

        ipServidor.setToolTipText("Formato (IP): desde 0.0.0.0 a 255.255.255.255");

        interfacesRed.setMaximumRowCount(6);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Tipo de dirección IP a utilizar:");

        grupo.add(tipoIPv4);
        tipoIPv4.setSelected(true);
        tipoIPv4.setText("IPv4");

        grupo.add(tipoIPv6);
        tipoIPv6.setText("IPv6");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ipServidor, javax.swing.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE)
                    .addComponent(interfacesRed, 0, 498, Short.MAX_VALUE)
                    .addComponent(tipoIPv4)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tipoIPv6)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(puertoRMILocal)
                                .addComponent(puertoRMIServidor, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)))
                        .addGap(0, 413, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(puertoRMIServidor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(puertoRMILocal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(ipServidor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(interfacesRed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tipoIPv4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tipoIPv6)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup grupo;
    private javax.swing.JComboBox interfacesRed;
    private javax.swing.JTextField ipServidor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField puertoRMILocal;
    private javax.swing.JTextField puertoRMIServidor;
    private javax.swing.JCheckBox tipoIPv4;
    private javax.swing.JCheckBox tipoIPv6;
    // End of variables declaration//GEN-END:variables
}
